package org.misty.seckill.service.impl;

import org.misty.seckill.dao.UserDao;
import org.misty.seckill.model.User;
import org.misty.seckill.redis.RedisService;
import org.misty.seckill.redis.SessionKey;
import org.misty.seckill.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
  @Autowired
  private UserDao userDao;
  @Autowired
  private RedisService redisService;

  @Override
  public User getById(long id) {
    return userDao.getById(id);
  }

  @Override
  public User getByToken(String token) {
    if (StringUtils.isEmpty(token)) {
      return null;
    }
    return redisService.get(SessionKey.TOKEN, token, User.class);
  }

  @Override
  public User insertUser(User user) {
    userDao.insertUser(user);
    return user;
  }

  @Override
  public List<User> listUsers() {
    return userDao.listUsers();
  }
}
